<?php
/**
 * Created by Command.
 * User: Vito
 */

declare (strict_types = 1);

namespace app\model;

class Housing extends BaseModel
{
    const SEX_UNKNOWN = 0;
    const SEX_MALE = 1;
    const SEX_FEMALE = 2;

    protected $sexEnum = [
        self::SEX_UNKNOWN => '未知',
        self::SEX_MALE    => '男',
        self::SEX_FEMALE  => '女',
    ];

    const TYPE_UNKNOWN  = 0;
    const TYPE_NAME     = 1;
    const TYPE_ADDRESS  = 2;

    protected $typeEnum = [
        self::TYPE_UNKNOWN => '请选择',
        self::TYPE_NAME    => '广东',
        self::SEX_FEMALE   => '汕头'
    ];

    // 一对一关联用户模型
    public function  oneUser()
    {
        // return $this->hasOne('User', 'uid')->bind([
        //     'truename' => 'nickname',
        // ]);
         return $this->hasOne('User','uid');
    }
    /**
     * @Description: 测试一对一模型关联
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-10-20 11:20:09
     * @version: 1.0.0
     * @return [type]
     */
    public static function getUser(){
        // $user = Housing::hasWhere('oneUser',['nickname' => '夏愚'])->select();
        // $user = Housing::withJoin(['user'  => ['uid','nickname', 'user_id']])->select();
        // $user = User::where('nickname','=','夏愚')->select();
        // $user = self::find(23);
        // $user->user->save(['user_id' => '1818']);
        // $user = $this->User::where(['nickname','夏愚'])->find();
        // $where['name']  = '夏愚';
        // $user = self::where($where)->find()->bindAttr('oneUser',[
        //     'nickname',
        //     'uids'=>'uid'
        // ]);
        
        // $user = self::where($where)->find();
        // $user->oneUser;

        // 写入数据

        $data = [
            // 'name'=>'零一',
            'nickname'=>'老黄'
        ];
        
        $user = new Housing; //实例化User模型并给属性赋值
        $user->address = 'piter';
        $user->uid = '1122';
        $user->create_time = time();
        $user->update_time = time();
        $user->delete_time = time();
        $user->mark = '123@my.com';
        $user->user_id = '1122';
        return $user->together(['oneUser' => ['uid','mark','user_id']])->save(); //关联写入

        // $user = self->together(['oneUser'])->save($data);
        return $user;
    }

    /**
     * @Description: 一对多关联用户模型
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-10-20 14:32:19
     * @version: 1.0.0
     * @return [type]
     */    
    public function manyUser(){

        return $this->hasMany('User','uid');
    }

    public static function getManyUser(){
        //查询
        // $where['name']  = '灵';
        // $user = self::where($where)->find();
        // $optionWhere['user_id'] = 1818;
        // $res =  $user->manyUser()->where($optionWhere)->select();

        // 条件查询
        // $res = self::has('manyUser', '>=', 3)->select();
        // 新增一条数据
        // $res = $user->manyUser()->save(['mark'=>'测试测试']);

        $user = self::with('manyUser')->find(25);

        $res = $user->together(['manyUser'])->delete();
        return $res;
    }
    
    
    public static function scopeTest(){
        $res = Housing::scope('Tst')->find();
        // var_dump($res);
        return $res;
    }
    /**
     * @Description:    封装范围查询
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-10-20 17:36:55
     * @version: 1.0.0
     * @return [type]
     */
    public function  scopeTst($query)
    {
        $query->where('name', '灵');
    }

    public function children(){
        return $this->hasMany('building', 'housing_id');
    }
    /**
     * @Description: 获取小区名称
     * @author: xiayu <cb1071403016@126.com>
     * @param [type] $where
     * @Date: 2022-11-01 14:39:50
     * @version: 1.0.0
     * @return [type]
     */
    public static function getHousing($where){
        return self::where($where)->value('name');
    }

    /**
     * @Description: 获取全部小区
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-01 14:42:00
     * @version: 1.0.0
     * @return [type]
     */
    public static function getDetail(){
        return self::select();
    }
}
